<template>
  <div>
    <slot />
  </div>
</template>
<script>
import mixin from "./mixin";
import {
  KmlDataSource,
  Color,
  JulianDate,
  Ellipsoid,
  BoundingSphere,
  HorizontalOrigin,
  VerticalOrigin,
} from "cesium";
export default {
  name: "EntityKml",
  mixins: [mixin],
  data() {
    this.entityType = "kml";
    return {
      kmlConfig: null,
    };
  },
  mounted() {},
  methods: {
    loadGzmodel(kmlSource, colorLists) {
      const viewer = this.$parent.viewer;
      var kmlOptions = {
        camera: viewer.scene.camera,
        canvas: viewer.scene.canvas,
        clampToGround: true,
      };
      this.kmlConfig = viewer.dataSources.add(
        new KmlDataSource.load(kmlSource, kmlOptions)
      );
      this.kmlConfig.then(function (dataSource) {
        var entities = dataSource.entities.values;
        var colorHash = {};
        //定义不同地区颜色值
        let colorList = colorLists
          ? colorLists
          : [
              "#bee8ff",
              "#f5a27a",
              "#bdd2ff",
              "#7ab6f5",
              "#f5ca7b",
              "#e8befe",
              "#ffeabd",
              "#bee8ff",
              "#ffa77f",
              "#febebe",
              "#0475bc",
              "#4a6ac5",
            ];
        entities.forEach((item, index) => {
          let entity = entities[index];
          let name = entity.name;
          let color = colorHash[name];
          if (!color) {
            color = Color.fromCssColorString(
              //   "#000"
              entities.length > colorList.length
                ? "#" + Math.random().toString(16).slice(2, 8)
                : colorList[index]
            ).withAlpha(0.5);
            colorHash[name] = color;
          }
          //背景填充
          entity.polygon.material = colorHash[name];

          //开始
          var polyPositions = entity.polygon.hierarchy.getValue(
            JulianDate.now()
          ).positions;
          var polyCenter = BoundingSphere.fromPoints(polyPositions).center;
          polyCenter = Ellipsoid.WGS84.scaleToGeodeticSurface(polyCenter);
          entity.position = polyCenter;
          entity.polygon.outlineWidth = 0;
          entity.polygon.fill = true;
          entity.polygon.outline = true;
          entity.polygon.outlineColor =
            Color.fromCssColorString("#0098cf").withAlpha(0.1);
          entity.polyline = {
            positions: entity.polygon.hierarchy._value.positions,
            width: entity.polygon.outlineWidth,
            material: Color.fromCssColorString("#0098cf").withAlpha(0.1),
          };

          // 结束

          if (name == "广州市") {
          } else {
            // entity.label = {
            //   text: name,
            //   color: Color.fromCssColorString("#f0f0f0"),
            //   font: "30px sans-serif",
            //   showBackground: true,
            //   scale: 0.6,
            //   horizontalOrigin: HorizontalOrigin.LEFT_CLICK,
            //   verticalOrigin: VerticalOrigin.BOTTOM,
            //   // distanceDisplayCondition : new that.$Cesium.DistanceDisplayCondition(10.0, 100000.0),
            //   // disableDepthTestDistance : 10000.0
            // };
          }
        });
      });
    },
  },
};
</script>
